package com.redstar.interesting.common.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author lihongxing
 */
@Slf4j
public class PinyinUtil {

    public static String[] getFormattedHanYuPinyinStringArray(char str) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        try {
            return PinyinHelper.toHanyuPinyinStringArray(str, format);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            log.info("汉字\"{}\"获取拼音失败", str);
        }
        return new String[0];
    }

    public static void main(String[] args) {
        String s = "啊我是歌词哈哈哈";

        for (char c : s.toCharArray()) {
//            String[] hanYuPinYing = getHanYuPinYing(c);
//            System.out.printf(hanYuPinYing[0]+" ");

            String[] array = getFormattedHanYuPinyinStringArray(c);
            System.out.printf(array[0]+" ");

        }

    }




}
